package com.shengyuan.manage_os.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.im.common.CCPAppManager;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;
import com.shengyuan.manage_os.sqlite.SQLiteTools;
import com.shengyuan.manage_os.view.CircleImageView;

import java.io.ByteArrayOutputStream;

import oa.database.GreenDaoHelper;
import oa.database.bean.Department;
import oa.database.bean.Employee;
import oa.database.bean.Friends;

/**
 * Created by Administrator on 2016/11/22.
 */

public class AddFirendActivity extends Activity implements View.OnClickListener {

    private CircleImageView circleImageView;
    private EditText phone, name_tv, bumen,bumenName;
//    private Button save, add_group,updatahear;
    private Button updatahear;
    private SQLiteTools db;
    Bitmap bitmap;

//    private List<Friends> friends;
//    private List<Employee> employees;
//    private Employee employee;
//    private List<Department> departments;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    private Uri uri;
    private int flag = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置状态栏为透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.activity_addfirend);
        db = new SQLiteTools(this);
//        friends = new ArrayList<>();
//        employees = new ArrayList<>();
//        departments = new ArrayList<>();
        circleImageView = (CircleImageView) findViewById(R.id.hear);
//        phone = (EditText) findViewById(R.id.phone);
//        bumen = (EditText) findViewById(R.id.bumenid);
//        bumenName = (EditText) findViewById(R.id.bumenName);
        updatahear = (Button) findViewById(R.id.updata_hear);
        updatahear.setOnClickListener(this);
//        name = (EditText) findViewById(R.id.name);
//        save = (Button) findViewById(R.id.save);
//        add_group = (Button) findViewById(R.id.add_group);
        circleImageView.setOnClickListener(this);
//        save.setOnClickListener(this);
//        add_group.setOnClickListener(this);

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }





    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.hear:
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 200);
                break;
            case R.id.updata_hear:
                if (bitmap == null){
                    Utility.MessageBox(this,"您还没有选择头像");
                    break;
                }
               Employee employee = SQLiteTools.queryAssignEmployee(CCPAppManager.getClientUser().getUserId());
                employee.setHearImage(Bitmap2StrByBase64(bitmap));
                SQLiteTools.upDataAssignEmployee(employee);
                setResult(1000);
                finish();
                break;

        }

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 200) {
            if (data != null) {
                // 得到图片的全路径
                uri = data.getData();
                crop(uri);
            }

        }
//        else if (requestCode == PHOTO_REQUEST_CAMERA) {
//            if (hasSdcard()) {
//                tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);
//                crop(Uri.fromFile(tempFile));
//            } else {
//                Toast.makeText(CenterUI.this, "未找到存储卡，无法存储照片！", Toast.LENGTH_SHORT).show();
//            }
//
//        }
        else if (requestCode == 100) {
            try {
                bitmap = data.getParcelableExtra("data");
                this.circleImageView.setImageBitmap(bitmap);//显示裁剪的图片
//                upload(Bitmap2StrByBase64(bitmap), filename);//将BASE64字符串发送到服务器
//                boolean delete = tempFile.delete();
//                System.out.println("delete = " + delete);


            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void crop(Uri uri) {
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例，1：1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        // 图片格式
        intent.putExtra("outputFormat", "JPEG");
        intent.putExtra("noFaceDetection", true);// 取消人脸识别
        intent.putExtra("return-data", true);// true:不返回uri，false：返回uri
        startActivityForResult(intent, 100);



    }

    public String Bitmap2StrByBase64(Bitmap bit) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bit.compress(Bitmap.CompressFormat.JPEG, 80, bos);//参数100表示不压缩
        byte[] bytes = bos.toByteArray();
        return Base64.encodeToString(bytes, Base64.DEFAULT);
    }


}
